import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () =>
  import(/* webpackChunkName:"Login_Home_Welcome" */ '../components/Login.vue')
const Home = () =>
  import(/* webpackChunkName:"Login_Home_Welcome" */ '../components/Home.vue')
const Welcome = () =>
  import(/* webpackChunkName:"Login_Home_Welcome" */ '../components/Welcome.vue')
const Users = () =>
  import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/user/Users.vue')
const Categories = () =>
  import(/* webpackChunkName:"Categories_Params" */ '../components/commodity/Categories.vue')
const Goods = () =>
  import(/* webpackChunkName:"Goods_Add" */ '../components/commodity/Goods.vue')
const Params = () =>
  import(/* webpackChunkName:"Categories_Params" */ '../components/commodity/Params.vue')
const Reports = () =>
  import(/* webpackChunkName:"Reports_Orders" */ '../components/data/Reports.vue')
const Orders = () =>
  import(/* webpackChunkName:"Reports_Orders" */ '../components/orderform/Orders.vue')
const Rights = () =>
  import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () =>
  import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Roles.vue')
const Add = () =>
  import(/* webpackChunkName:"Goods_Add" */ '../components/commodity/Add.vue')
Vue.use(VueRouter)

const routes = [{
  path: '/login',
  component: Login
}, {
  path: '/',
  redirect: '/login'
}, {
  path: '/home',
  component: Home,
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: Welcome
  }, {
    path: '/users',
    component: Users
  }, {
    path: '/categories',
    component: Categories
  }, {
    path: '/goods',
    component: Goods
  }, {
    path: '/params',
    component: Params
  }, {
    path: '/reports',
    component: Reports
  }, {
    path: '/orders',
    component: Orders
  }, {
    path: '/rights',
    component: Rights
  }, {
    path: '/roles',
    component: Roles
  }, {
    path: '/goods/add',
    component: Add
  }]
}]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to要访问的路径
  // from代表从哪个路径跳转而来
  // next是个函数，表示放行
  // next()表示放行
  // next('/login')强制跳转到login组件
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const TokenStr = window.sessionStorage.getItem('token')
  // 如果没有token值，那便是处于未登录状态，强制跳转到'/login'
  if (!TokenStr) {
    return next('/login')
  }
  next()
})

export default router
